package com.maya.medicalwastes.ui;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.maya.medicalwastes.R;
import com.maya.medicalwastes.util.CustomProber;

import java.util.ArrayList;
import java.util.Locale;

import timing.ukulele.serialport.driver.UsbSerialDriver;
import timing.ukulele.serialport.driver.UsbSerialProber;
public class BluetoothActivity extends ListActivity {
    class ListItem {
        UsbDevice device;
        int port;
        UsbSerialDriver driver;

        ListItem(UsbDevice device, int port, UsbSerialDriver driver) {
            this.device = device;
            this.port = port;
            this.driver = driver;
        }
    }

    private ArrayList<ListItem> listItems = new ArrayList<>();
    private ArrayAdapter<ListItem> listAdapter;
    private int baudRate = 9600;
    private boolean withIoManager = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View header = getLayoutInflater().inflate(R.layout.device_list_header, null, false);
        getListView().addHeaderView(header, null, false);
        listAdapter = new ArrayAdapter<ListItem>(this, 0, listItems) {
            @Override
            public View getView(int position, View view, ViewGroup parent) {
                ListItem item = listItems.get(position);
                if (view == null)
                    view = getLayoutInflater().inflate(R.layout.device_list_item, parent, false);
                TextView text1 = view.findViewById(R.id.text1);
                TextView text2 = view.findViewById(R.id.text2);
                if (item.driver == null)
                    text1.setText("<no driver>");
                else if (item.driver.getPorts().size() == 1)
                    text1.setText(item.driver.getClass().getSimpleName().replace("SerialDriver", ""));
                else
                    text1.setText(item.driver.getClass().getSimpleName().replace("SerialDriver", "") + ", Port " + item.port);
                text2.setText(String.format(Locale.US, "Vendor %04X, Product %04X", item.device.getVendorId(), item.device.getProductId()));
                return view;
            }
        };
        setListAdapter(listAdapter);
        refresh();
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ListItem item = listItems.get(position - 1);
        if (item.driver == null) {
            Toast.makeText(this, "no driver", Toast.LENGTH_SHORT).show();
        } else {

        }
    }
    void refresh() {
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
        UsbSerialProber usbCustomProber = CustomProber.getCustomProber();
        listItems.clear();
        for (UsbDevice device : usbManager.getDeviceList().values()) {
            UsbSerialDriver driver = usbDefaultProber.probeDevice(device);
            if (driver == null) {
                driver = usbCustomProber.probeDevice(device);
            }
            if (driver != null) {
                driver.getDevice().getProductName().contains("USB");
                Bundle args = new Bundle();
                args.putInt("device", device.getDeviceId());
                args.putInt("port", 0);
                args.putInt("baud", baudRate);
                args.putBoolean("withIoManager", withIoManager);
                Intent intent = new Intent(BluetoothActivity.this, DemoActivity.class);
                intent.putExtras(args);
                startActivity(intent);
//                for (int port = 0; port < driver.getPorts().size(); port++)
//                    listItems.add(new ListItem(device, port, driver));
//            } else {
//                listItems.add(new ListItem(device, 0, null));
//            }
            }
        }
  //      listAdapter.notifyDataSetChanged();
    }

}
